package common.net
{
    import asset.global.ErrorImageAsset;
    
    import flash.display.BitmapData;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.system.SecurityDomain;
    
    import kernel.cdn.CDN;
    import kernel.core.Core;
    
    import org.granite.net.AbstractAssetReferenceManager;
    import org.granite.net.ILoader;
    import org.granite.net.LoaderConfiguration;
    import org.granite.net.LoaderQueue;
    import org.granite.util.ReferenceManager;



    public class URLImageManager extends AbstractAssetReferenceManager
    {
        public static const instance:URLImageManager = new URLImageManager ();

        public function URLImageManager():void
        {
            super (new LoaderQueue (3 , 40) , ReferenceManager.COUNT_REFERENCE);
        }

        public override function createLoaderConfiguration(path:String):LoaderConfiguration
        {
            var lconfig:LoaderConfiguration = new LoaderConfiguration ();
            lconfig.loader = new ImageLoader ();
            lconfig.request = new URLRequest (path);
            lconfig.context = loaderContext;
            return lconfig;
        }

        private function get loaderContext():LoaderContext
        {
            return new LoaderContext (false , ApplicationDomain.currentDomain , Core.isLocal ? null : SecurityDomain.currentDomain);
        }

        protected override function _getLoadedData(path:String , loader:ILoader , error:Boolean):*
        {
            if (error)
            {
                return new ErrorImageAsset ();
            }
            else
            {
                return loader.data as BitmapData;
            }
        }
    }
}
